package leetcode.code2223;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public long sumScores(String s) {
		int len = s.length();
		int[] z = new int[len];
		int L = 0, R = 0, r = 0;
		long ans = len;
		for (int i = 1; i < len; i++) {
			z[i] = i > R ? 0 : Math.min(z[i - L], R - i + 1);
			while ((r = i + z[i]) < len && s.charAt(z[i]) == s.charAt(r)) {
				L = i;
				R = r;
				z[i]++;
			}
			ans += z[i];
		}
		return ans;
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		H.compare(9, this.sumScores("babab"));
		H.compare(14, this.sumScores("azbazbzaz"));

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
